package com.dongzili.demo.customer.fulltext.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongzili.demo.common.config.mybatisplus.MybatisPlusUtil;
import com.dongzili.demo.common.vo.ApiResponse;
import com.dongzili.demo.customer.fulltext.service.RedisFullTextUtils;
import com.dongzili.demo.customer.redis.model.Relation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @描述：
 * @作者： 96511
 * @日期： 2025-01-04 19:16
 */
@RestController
@RequestMapping("/fulltext")
public class RedisFullTextController {


    @GetMapping("/syncIndex")
    public ApiResponse<?> syncIndex(){
        try {
            RedisFullTextUtils.getService(Relation.class).syncIndex();
            return ApiResponse.success();
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }

//    @GetMapping("/sync")
//    public ApiResponse<?> sync(){
//        int current = 1;
//        int size = 2000;
//        Page<Relation> page = MybatisPlusUtil.getMapper(Relation.class).selectPage(new Page<>(current, size), new LambdaQueryWrapper<>());
//        List<Relation> list = page.getRecords();
//        for (Relation relation : list){
//            RedisFullTextUtils.getService(Relation.class).save(relation);
//        }
//
//        for (int i = 2; i <= page.getPages(); i++){
//            LambdaQueryWrapper<Relation> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.ge(Relation::getId, (i-1)*size);
//            queryWrapper.lt(Relation::getId, i*size);
//            List<Relation> currentList = MybatisPlusUtil.getMapper(Relation.class).selectList(queryWrapper);
//            page = MybatisPlusUtil.getMapper(Relation.class).selectPage(new Page<>(i, size), new LambdaQueryWrapper<>());
//            list = page.getRecords();
//            for (Relation relation : list){
//                RedisFullTextUtils.getService(Relation.class).save(relation);
//            }
//        }
//
//        return ApiResponse.success();
//    }
    @GetMapping("/search")
    public ApiResponse<?> search(@RequestParam("text") String text, @RequestParam(value = "current", defaultValue = "1") int current, @RequestParam(value = "size", defaultValue = "10") int size){
        Page<Relation> page = RedisFullTextUtils.getService(Relation.class).search(text, current, size);
//        Page<Relation> page = new Page<>(current, list.size());
//        page.setRecords(list);
        return ApiResponse.success(page);
    }
//    @GetMapping("/search2")
//    public ApiResponse<?> search2(@RequestParam("text") String text){
//
//        List<Relation> list = RedisFullTextUtils.getService(Relation.class).search(text);
//        return ApiResponse.success(list);
//    }

}
